package com.github.zzay.care.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.zzay.care.dto.ElderlyDto;
import com.github.zzay.care.entity.Elderly;
import com.github.zzay.care.mapper.ElderlyMapper;
import com.github.zzay.care.service.ElderlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzay
 * @className ElderlyServiceImpl
 * @description Implementation class for "ElderlyService"
 * @create 2022/07/07 00:04
 */
@Service
public class ElderlyServiceImpl extends ServiceImpl<ElderlyMapper, Elderly> implements ElderlyService {

    /**
     * Elderly mapper.
     */
    @Autowired
    private ElderlyMapper elderlyMapper;

    /**
     * Delete an elderly person's information.
     *
     * @param idCard ID card number
     * @return Operation result
     */
    public Boolean deleteInfo(String idCard) {
        Map<String, Object> map = new HashMap<>();
        ElderlyDto elderly = elderlyMapper.selectByIdCard(idCard);
        if (elderly == null) {
            return false;
        }
        map.put("id_card", idCard);
        elderlyMapper.deleteByMap(map);
        return true;
    }

    /**
     * Update an elderly person's information.
     *
     * @param elderlyDto Elderly DTO
     * @return Operation result
     */
    public Boolean updateInfo(ElderlyDto elderlyDto) {
        ElderlyDto elderly = elderlyMapper.selectByIdCard(elderlyDto.getIdCard());
        if (elderly == null) {
            return false;
        }
        LambdaUpdateWrapper<Elderly> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Elderly::getIdCard, elderly.getIdCard())
                .set(Elderly::getRoomNumber, elderlyDto.getRoomNumber())
                .set(Elderly::getSex, elderlyDto.getSex())
                .set(Elderly::getPhone, elderlyDto.getPhone())
                .set(Elderly::getBirthday, elderlyDto.getBirthday())
                .set(Elderly::getHealthState, elderlyDto.getHealthState())
                .set(Elderly::getCheckInDate, elderlyDto.getCheckInDate())
                .set(Elderly::getCheckOutDate, elderlyDto.getCheckOutDate())
                .set(Elderly::getFirstGuardianName, elderlyDto.getFirstGuardianName())
                .set(Elderly::getFirstGuardianRelationship, elderlyDto.getFirstGuardianRelationship())
                .set(Elderly::getFirstGuardianPhone, elderlyDto.getFirstGuardianPhone())
                .set(Elderly::getFirstGuardianWechat, elderlyDto.getFirstGuardianWechat())
                .set(Elderly::getSecondGuardianName, elderlyDto.getSecondGuardianName())
                .set(Elderly::getSecondGuardianRelationship, elderlyDto.getSecondGuardianRelationship())
                .set(Elderly::getSecondGuardianPhone, elderlyDto.getSecondGuardianPhone())
                .set(Elderly::getSecondGuardianWechat, elderlyDto.getSecondGuardianWechat());
//        elderly.setRoomNumber(elderlyDto.getRoomNumber());
//        elderly.setSex(elderlyDto.getSex());
//        elderly.setPhone(elderlyDto.getPhone());
//        elderly.setBirthday(elderlyDto.getBirthday());
//        elderly.setHealthState(elderlyDto.getHealthState());
//        elderly.setCheckInDate(elderlyDto.getCheckInDate());
//        elderly.setCheckOutDate(elderlyDto.getCheckOutDate());
//        elderly.setProfilePhoto(elderlyDto.getProfilePhoto());
//        elderly.setImageDir(elderlyDto.getImageDir());
//        elderly.setFirstGuardianName(elderlyDto.getFirstGuardianName());
//        elderly.setFirstGuardianRelationship(elderlyDto.getFirstGuardianRelationship());
//        elderly.setFirstGuardianPhone(elderlyDto.getFirstGuardianPhone());
//        elderly.setFirstGuardianWechat(elderlyDto.getFirstGuardianWechat());
//        elderly.setSecondGuardianName(elderlyDto.getSecondGuardianName());
//        elderly.setSecondGuardianRelationship(elderlyDto.getSecondGuardianRelationship());
//        elderly.setSecondGuardianPhone(elderlyDto.getSecondGuardianPhone());
//        elderly.setSecondGuardianWechat(elderlyDto.getSecondGuardianWechat());
        return elderlyMapper.update(null, lambdaUpdateWrapper) != 0;
    }

    /**
     * Get all information.
     *
     * @return A list of all information
     */
    public List<ElderlyDto> getAll() {
        return elderlyMapper.selectAll();
    }

    /**
     * Get an elderly person's information with the given id card number.
     *
     * @param idCard ID card number
     * @return Elderly person's information
     */
    public ElderlyDto getInfoById(String idCard) {
        return elderlyMapper.selectByIdCard(idCard);
    }


    /**
     * Get an elderly person's information with the given name.
     *
     * @param name Name
     * @return Elderly person's information
     */
    public ElderlyDto getInfoByName(String name) {
        return elderlyMapper.selectByName(name);
    }

}
